package icraf.common.gui;

import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;

public class MenuGenerator {

	public enum IconModel {
		ICON16, ICON24, ICON32, ICON48;
	}

	public static JButton createButton(MenuAdapter p, ActionListener action,
			boolean isShowLabel, IconModel iconModel) {
		JButton mi = new JButton();
		mi.setMnemonic(p.getMnemonic());
		mi.setActionCommand(p.getActionCommand());
		if (isShowLabel) {
			mi.setText(p.getLabel());
			mi.setToolTipText(p.getAccessibleDescription());
		} else {
			mi.setToolTipText(p.getLabel());
		}
		mi.addActionListener(action);
		ImageIcon icon = p.getIcon(iconModel);
		if (icon != null)
			mi.setIcon(icon);
		if (action == null) {
			mi.setEnabled(false);
		}
		return mi;
	}

	public static JButton createButtonIcon(MenuAdapter p, ActionListener action) {
		return createButton(p, action, false, IconModel.ICON16);
	}

	public static JButton[] createButtonIcon(MenuAdapter piList[],
			ActionListener action) {
		JButton[] miList = new JButton[piList.length];
		for (int i = 0; i < piList.length; i++) {
			if (piList[i] != null)
				miList[i] = createButtonIcon(piList[i], action);
		}
		return miList;
	}

	/**
	 * Creates a generic menu item
	 */
	public static JMenuItem createMenuItem(MenuAdapter p, ActionListener action) {
		JMenuItem mi = new JMenuItem(p.getLabel());
		mi.setMnemonic(p.getMnemonic());
		mi.setActionCommand(p.getActionCommand());
		mi.getAccessibleContext().setAccessibleDescription(
				p.getAccessibleDescription());
		mi.addActionListener(action);
		ImageIcon icon = p.getIcon(IconModel.ICON16);
		if (icon != null)
			mi.setIcon(icon);
		if (action == null) {
			mi.setEnabled(false);
		}
		return mi;
	}

	/**
	 * Creates a generic menu item list
	 */
	public static JMenuItem[] createMenuItem(MenuAdapter piList[],
			ActionListener action) {
		JMenuItem[] miList = new JMenuItem[piList.length];
		for (int i = 0; i < piList.length; i++) {
			if (piList[i] != null)
				miList[i] = createMenuItem(piList[i], action);
		}
		return miList;
	}

	public static JMenu createMenuItem(JMenu menu, MenuAdapter piList[],
			ActionListener action) {
		JMenuItem[] miList = createMenuItem(piList, action);
		for (JMenuItem mi : miList) {
			if (mi != null) {
				menu.add(mi);
			} else {
				menu.addSeparator();
			}
		}
		return menu;
	}

	public static JPopupMenu createMenuItem(JPopupMenu menu,
			MenuAdapter piList[], ActionListener action) {
		JMenuItem[] miList = createMenuItem(piList, action);
		for (JMenuItem mi : miList) {
			if (mi != null) {
				menu.add(mi);
			} else {
				menu.addSeparator();
			}
		}
		return menu;
	}

	public static JToolBar createMenuItem(JToolBar menu, MenuAdapter piList[],
			ActionListener action) {
		JButton[] miList = createButtonIcon(piList, action);
		for (JButton mi : miList) {
			if (mi != null) {
				menu.add(mi);
			} else {
				menu.addSeparator();
			}
		}
		return menu;
	}

	public static JRadioButton createRadioButton(MenuAdapter p,
			ActionListener action) {
		return createRadioButton(p, action, null);
	}

	public static JRadioButton createRadioButton(MenuAdapter p,
			ActionListener action, ButtonGroup bg) {
		JRadioButton mi = new JRadioButton();
		mi.setMnemonic(p.getMnemonic());
		mi.setActionCommand(p.getActionCommand());
		mi.setText(p.getLabel());
		mi.setToolTipText(p.getAccessibleDescription());
		mi.addActionListener(action);
		if (bg != null)
			bg.add(mi);
		return mi;
	}

	public static JRadioButton createRadioButtonIcon(MenuAdapter p,
			ActionListener action, IconModel iconModel, boolean isLabel) {
		JRadioButton mi = new JRadioButton();
		mi.setMnemonic(p.getMnemonic());
		mi.setActionCommand(p.getActionCommand());
		if (isLabel)
			mi.setText(p.getLabel());
		mi.setToolTipText(p.getAccessibleDescription());
		mi.addActionListener(action);
		ImageIcon icon = p.getIcon(iconModel);
		if (icon != null)
			mi.setIcon(icon);
		return mi;
	}

	public static JRadioButtonMenuItem createRadioButtonMenuItem(MenuAdapter p,
			ActionListener action) {
		JRadioButtonMenuItem mi = new JRadioButtonMenuItem(p.getLabel());
		mi.setMnemonic(p.getMnemonic());
		mi.setActionCommand(p.getActionCommand());
		mi.getAccessibleContext().setAccessibleDescription(
				p.getAccessibleDescription());
		mi.addActionListener(action);
		ImageIcon icon = p.getIcon(IconModel.ICON16);
		if (icon != null)
			mi.setIcon(icon);
		if (action == null) {
			mi.setEnabled(false);
		}
		return mi;
	}

	public static JCheckBoxMenuItem createCheckBoxMenuItem(MenuAdapter p,
			ActionListener action) {
		JCheckBoxMenuItem mi = new JCheckBoxMenuItem(p.getLabel());
		mi.setMnemonic(p.getMnemonic());
		mi.setActionCommand(p.getActionCommand());
		mi.getAccessibleContext().setAccessibleDescription(
				p.getAccessibleDescription());
		mi.addActionListener(action);
		ImageIcon icon = p.getIcon(IconModel.ICON16);
		if (icon != null)
			mi.setIcon(icon);
		if (action == null) {
			mi.setEnabled(false);
		}
		return mi;
	}

	public static JCheckBox createCheckBox(MenuAdapter m, ActionListener action) {
		JCheckBox mi = new JCheckBox(m.getLabel());
		mi.setMnemonic(m.getMnemonic());
		mi.setActionCommand(m.getActionCommand());
		mi.getAccessibleContext().setAccessibleDescription(
				m.getAccessibleDescription());
		mi.addActionListener(action);
		ImageIcon icon = m.getIcon(IconModel.ICON16);
		if (icon != null)
			mi.setIcon(icon);
		if (action == null) {
			mi.setEnabled(false);
		}
		return mi;
	}

	public static JMenu createRadioButtonSubMenuItem(MenuAdapter headMenu,
			MenuAdapter piList[], ActionListener action) {
		JMenu hm = new JMenu(headMenu.getLabel());
		hm.setIcon(headMenu.getIcon(IconModel.ICON16));
		ButtonGroup group = new ButtonGroup();
		for (int i = 0; i < piList.length; i++) {
			group.add(hm.add(createRadioButtonMenuItem(piList[i], action)));
		}
		group.getElements().nextElement().setSelected(true);
		return hm;
	}

	/**
	 * Creates a generic menu item list
	 */
	public static JMenu createSubMenuItem(MenuAdapter headMenu,
			MenuAdapter piList[], ActionListener action) {
		JMenu hm = new JMenu(headMenu.getLabel());
		hm.setIcon(headMenu.getIcon(IconModel.ICON16));
		for (int i = 0; i < piList.length; i++) {
			hm.add(createMenuItem(piList[i], action));
		}
		return hm;
	}

	public static JToggleButton createToggleButton(MenuAdapter p,
			ActionListener action, boolean label, IconModel iconModel) {
		JToggleButton mi = new JToggleButton();
		mi.setMnemonic(p.getMnemonic());
		mi.setActionCommand(p.getActionCommand());
		if (label) {
			mi.setText(p.getLabel());
			mi.setToolTipText(p.getAccessibleDescription());
		} else {
			mi.setToolTipText(p.getLabel());
		}
		mi.addActionListener(action);
		ImageIcon icon = p.getIcon(iconModel);
		if (icon != null)
			mi.setIcon(icon);
		if (action == null) {
			mi.setEnabled(false);
		}
		return mi;
	}

	public static JToolBar createToolBarButton(JToolBar menu,
			MenuAdapter piList[], ActionListener action) {
		JButton[] miList = createButtonIcon(piList, action);
		for (JButton mi : miList) {
			menu.add(mi);
		}
		return menu;
	}

	public MenuGenerator() {
		super();
	}

}